/*
 * @Autor: dingxiaolin
 * @Date: 2021-06-09 15:18:42
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2021-07-27 19:44:56
 */
import modules from '@/store/modules'
import { createStore, useStore as baseUseStore, createLogger, Store } from 'vuex'
import createPersistedState from 'vuex-persistedstate';
import { App, InjectionKey } from 'vue'
import { IStore } from './types'
const process = import.meta.env
export const key: InjectionKey<Store<IStore>> = Symbol()

// 在开发环境中开启logger
const debug = false
// const debug = process.env.NODE_ENV !== 'production'

const plugins = debug
    ? [
        createLogger({
            filter(mutation, stateBefore, stateAfter) {
                // 若 mutation 需要被记录，就让它返回 true 即可
                // 顺便，`mutation` 是个 { type, payload } 对象
                const notNeedDebugs: string[] = ['lockscreen/setLockTime', 'lockscreen/setLock']
                return !notNeedDebugs.includes(mutation.type)
            }
        })
    ]
    : []
// 可以根据自己需要添加其他插件，例如：vuex持久化插件
plugins.push(createPersistedState({ storage: window.sessionStorage }));

const store = createStore<IStore>({
    plugins,
    modules
})

// 定义你自己的“useStore”组合函数
export function useStore() {
    return store
    // return baseUseStore(key)
}

export function setupStore(app: App) {
    app.use(store, key)
    app.config.globalProperties.$store = store
    window.process = process
}

export default store
